#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <unistd.h>
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#define max 6

int main ( int argc, char** argv )
{
    int pos=0,k,pos1=0,l=(-1),p;
    int i,t[64][2];
    int cpt=0,ca=0,cn=0,ci=0;
    int page=0;

    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    atexit(SDL_Quit);

    SDL_Surface* screen = SDL_SetVideoMode(980, 700, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Surface* bmpp;
    SDL_Surface* bmp02 = SDL_LoadBMP("wazza3.bmp");
    SDL_Surface* bmp01 = SDL_LoadBMP("wazza2.bmp");
    SDL_Surface* bmp03 = SDL_LoadBMP("ins1.bmp");
    SDL_Surface* bmp04 = SDL_LoadBMP("ins2.bmp");
    SDL_Surface* bmp05 = SDL_LoadBMP("ins3.bmp");
    SDL_Surface* bmp06 = SDL_LoadBMP("ins4.bmp");

    SDL_Surface* bmp0 = SDL_LoadBMP("wazza.bmp");
    SDL_Surface* bmp7 = SDL_LoadBMP("f2.bmp");
    SDL_Surface* bmp8 = SDL_LoadBMP("maps.bmp");
    SDL_Surface* bmp9 = SDL_LoadBMP("oie.bmp");
    SDL_SetColorKey(bmp9, SDL_SRCCOLORKEY, SDL_MapRGB(bmp9->format, 255, 255, 255));
    SDL_Surface* bmp19 = SDL_LoadBMP("oie.bmp");
    SDL_SetColorKey(bmp19, SDL_SRCCOLORKEY, SDL_MapRGB(bmp19->format, 255, 255, 255));
    SDL_Surface* bmp10 = SDL_LoadBMP("AAA.bmp");
    SDL_Surface* bmp12 = SDL_LoadBMP("ds.bmp");
    SDL_SetColorKey(bmp12, SDL_SRCCOLORKEY, SDL_MapRGB(bmp12->format, 30, 73, 155));

    SDL_Surface* bmp13 = SDL_LoadBMP("d1.bmp");
    SDL_Surface* bmp14 = SDL_LoadBMP("d2.bmp");
    SDL_Surface* bmp15 = SDL_LoadBMP("d3.bmp");
    SDL_Surface* bmp16 = SDL_LoadBMP("d4.bmp");
    SDL_Surface* bmp17 = SDL_LoadBMP("d5.bmp");
    SDL_Surface* bmp18 = SDL_LoadBMP("d6.bmp");
    SDL_Surface* bmp1 = SDL_LoadBMP("oiet.bmp");
    SDL_SetColorKey(bmp1, SDL_SRCCOLORKEY, SDL_MapRGB(bmp1->format, 255, 255, 255));
    SDL_Surface* bmp20 = SDL_LoadBMP("1.bmp");
    SDL_SetColorKey(bmp20, SDL_SRCCOLORKEY, SDL_MapRGB(bmp20->format, 0, 0, 0));

    SDL_Surface* bmp21 = SDL_LoadBMP("2.bmp");
    SDL_SetColorKey(bmp21, SDL_SRCCOLORKEY, SDL_MapRGB(bmp21->format, 0, 0, 0));

    SDL_Surface* bmp22 = SDL_LoadBMP("100.bmp");
    SDL_SetColorKey(bmp22, SDL_SRCCOLORKEY, SDL_MapRGB(bmp22->format, 0, 0, 0));






    if (!bmp0 || !bmp1 || !bmp7 || !bmp8 || !bmp9 || !bmp10 ||!bmp12 || !bmp13 || !bmp14 || !bmp15 || !bmp16 || !bmp17 || !bmp18 || !bmp01 )

    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }


    SDL_Rect dstrect03;
    dstrect03.x = 0;
    dstrect03.y = 0;

    SDL_Rect dstrect0;
    dstrect0.x = 0;
    dstrect0.y = 0;

    SDL_Rect dstrect01;
    dstrect01.x = 0;
    dstrect01.y = 0;

    SDL_Rect dstrect02;
    dstrect02.x = 0;
    dstrect02.y = 0;

    SDL_Rect dstrect7;
    dstrect7.x = 0;
    dstrect7.y = 0;

    SDL_Rect dstrect8;
    dstrect8.x = 0;
    dstrect8.y = 0;

    SDL_Rect dstrect9;
    dstrect9.x = 60;
    dstrect9.y = 0;

    SDL_Rect dstrect19;
    dstrect19.x = 50;
    dstrect19.y = 7;

    SDL_Rect dstrect10;
    dstrect10.x = 0;
    dstrect10.y = 0;

    SDL_Rect dstrect13;
    dstrect13.x = 700;
    dstrect13.y = 540;

    SDL_Rect dstrect14;
    dstrect14.x = 700;
    dstrect14.y = 540;

    SDL_Rect dstrect15;
    dstrect15.x = 700;
    dstrect15.y = 540;

    SDL_Rect dstrect16;
    dstrect16.x = 700;
    dstrect16.y = 540;

    SDL_Rect dstrect17;
    dstrect17.x = 700;
    dstrect17.y = 540;

    SDL_Rect dstrect18;
    dstrect18.x = 700;
    dstrect18.y = 540;

    SDL_Rect dstrect12;
    dstrect12.x = 490;
    dstrect12.y = 520;

    SDL_Rect dstrect1;
    dstrect1.x = 330;
    dstrect1.y = 250;

    SDL_Rect dstrect20;
    dstrect20.x = 288;
    dstrect20.y = 538;

    SDL_Rect dstrect21;
    dstrect21.x = 290;
    dstrect21.y = 540;

    SDL_Rect dstrect22;
    dstrect22.x = 1050;
    dstrect22.y = 537;


    bool done = false;
    while (!done)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = true;
                break;

            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                {
                    {
                        if (page==1)
                        {
                            ca=0;
                            page=0;
                            dstrect1.x = 330;
                            dstrect1.y = 250;
                        }
                        else if (page==2)
                        {
                            ca=0;
                            page=1;
                            dstrect1.x = 216;
                            dstrect1.y = 187;
                        }
                        else if (page==4)
                        {
                            page=0;
                            ca=0;
                        }
                        else if(page==3)
                        {
                            page=0;
                            dstrect1.x = 470;
                            dstrect1.y = 22;
                            ca=0;
                        }
                        else if(page==5)
                        {
                            page=0;
                            dstrect1.x = 470;
                            dstrect1.y = 22;
                            ca=0;
                        }
                        else if (page==0)
                            done = true;
                    }
                    break;
                }
                else

                    if (event.key.keysym.sym == SDLK_DOWN)
                    {
                        if (page==0)
                        {
                            if(cpt==0)
                            {
                                dstrect1.y = 370;
                                cpt++;
                            }
                            else if(cpt==1)
                            {
                                dstrect1.y = 440;
                                cpt++;
                            }

                        }
                        if (page==2)
                        {
                            if(cn==0)
                            {
                                dstrect1.y = 181;
                                cn++;
                            }
                            else if(cn==1)
                            {
                                dstrect1.y = 239;
                                cn++;
                            }

                        }
                        break;
                    }
                    else  if (event.key.keysym.sym == SDLK_RIGHT)
                    {
                        if (page==1)
                        {

                            if (ca==0)
                            {
                                dstrect1.x = 314;
                                ca++;
                            }
                            else  if (ca==1)
                            {
                                dstrect1.x =407 ;
                                ca++;
                            }
                            else  if (ca==2)
                            {
                                dstrect1.x = 664;
                                ca++;
                            }
                            else if (ca==3)
                            {
                                dstrect1.x = 761;
                                ca++;
                            }
                            else  if (ca==4)
                            {
                                dstrect1.x = 858;
                                ca++;
                            }
                        }
                        else if(page==4)
                        {
                            if (ci==0)
                            {
                                bmpp=bmp04;
                                bmp04=bmp03;
                                bmp03=bmpp;
                                ci++;
                            }

                            else if (ci==1)
                            {
                                bmpp=bmp05;
                                bmp05=bmp04;
                                bmp04=bmpp;
                                ci++;
                            }

                            else if (ci==2)
                            {
                                bmpp=bmp03;
                                bmp03=bmp06;
                                bmp06=bmpp;
                                ci++;
                            }
                        }
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_UP)
                    {
                        if (page==0)
                        {
                            if(cpt==2)
                            {
                                dstrect1.y =370;
                                cpt--;
                            }
                            else if(cpt==1)
                            {
                                dstrect1.y = 260;
                                cpt--;
                            }
                        }
                        if (page==2)
                        {
                            if(cn==2)
                            {
                                dstrect1.y = 181;
                                cn--;
                            }
                            else if(cn==1)
                            {
                                dstrect1.y = 123;
                                cn--;
                            }
                        }

                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_LEFT)
                    {
                        if (page==1)
                        {

                            if (ca==1)
                            {
                                dstrect1.x = 216;
                                ca--;
                            }
                            else  if (ca==2)
                            {
                                dstrect1.x =314 ;
                                ca--;
                            }
                            else  if (ca==3)
                            {
                                dstrect1.x = 407;
                                ca--;
                            }
                            else if (ca==4)
                            {
                                dstrect1.x = 664;
                                ca--;
                            }
                            else  if (ca==5)
                            {
                                dstrect1.x = 761;
                                ca--;
                            }
                        }
                        else if (page==4)
                        {
                            if (ci==1)
                            {
                                bmpp=bmp04;
                                bmp04=bmp03;
                                bmp03=bmpp;
                                ci--;
                            }
                           else if (ci==2)
                            {
                                bmpp=bmp04;
                                bmp04=bmp05;
                                bmp05=bmpp;
                                ci--;
                            }
                           else if (ci==3)
                            {
                                bmpp=bmp03;
                                bmp03=bmp05;
                                bmp05=bmpp;
                                ci--;
                            }
                        }
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_RETURN)
                    {

                        if(cpt==0 && page==0)
                        {
                            page=1;
                            dstrect1.x = 216;
                            dstrect1.y = 187;
                        }
                        else if( page==1)
                        {
                            dstrect1.x = 500;
                            dstrect1.y = 123;
                            page=2;
                        }
                        else if( page==2)
                        {
                            page=3;
                        }
                        if(cpt==1 && page==0)
                        {
                            page=4;
                        }

                        else if (cpt==2 && page==0)
                        {
                            done = true;
                        }
                        break;
                    }

                    else if (event.key.keysym.sym == SDLK_SPACE)
                    {
                        l=l*(-1);
                        k=rand();
                        k=1+(k%6);

                        t[0][0]=0;
                        t[0][1]=60;
                        for (i=1; i<9; i++)
                        {
                            t[i][1]=t[i-1][1];
                            t[i][0]=t[i-1][0]+58;
                        }
                        for (i=9; i<18; i++)
                        {
                            t[i][0]=t[i-1][0];
                            t[i][1]=t[i-1][1]+118;
                        }
                        for (i=18; i<25; i++)
                        {
                            t[i][0]=t[i-1][0]-59;
                            t[i][1]=t[i-1][1];
                        }
                        for (i=25; i<33; i++)
                        {
                            t[i][0]=t[i-1][0];
                            t[i][1]=t[i-1][1]-118;
                        }
                        for (i=33; i<39; i++)
                        {
                            t[i][0]=t[i-1][0]+59;
                            t[i][1]=t[i-1][1];
                        }
                        for (i=39; i<46; i++)
                        {
                            t[i][0]=t[i-1][0];
                            t[i][1]=t[i-1][1]+118;
                        }
                        for (i=46; i<51; i++)
                        {
                            t[i][0]=t[i-1][0]-59;
                            t[i][1]=t[i-1][1];
                        }
                        for (i=51; i<57; i++)
                        {
                            t[i][0]=t[i-1][0];
                            t[i][1]=t[i-1][1]-118;
                        }
                        for (i=57; i<61; i++)
                        {
                            t[i][0]=t[i-1][0]+58;
                            t[i][1]=t[i-1][1];
                        }
                        for (i=61; i<64; i++)
                        {
                            t[i][0]=t[i-1][0];
                            t[i][1]=t[i-1][1]+118;
                        }
                        if (l<0)
                        {
                            if(pos>63)
                                pos=63-(pos%63);
                            else if(pos==63)
                            {
                                dstrect9.x=50;
                                dstrect9.y=7;
                                page=5;
                                pos=0;
                                pos1=0;
                            }
                        }
                        else if (l>0)

                        {
                            if(pos1>63)
                                pos1=63-(pos1%63);

                            else if(pos1==63)
                            {
                                dstrect19.x=50;
                                dstrect19.y=7;
                                page=5;
                                pos1=0;
                                pos=0;
                            }
                        }
                        if (l<0)
                            for (p=0; p<k; p++)
                            {
                                pos++;
                                dstrect9.x = t[pos][1];
                                dstrect9.y = t[pos][0];
                                //  usleep(200000);
                            }


                        else if (l>0)
                            for (p=1; p<k+1; p++)
                            {
                                pos1++;
                                dstrect19.x = t[pos1][1];
                                dstrect19.y = t[pos1][0];
                                //usleep(200000);
                            }
                    }
            }
            }
        }
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 181, 230, 29));

        if(page==0)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1245, 550, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(bmp0, 0, screen, &dstrect0);
            SDL_BlitSurface(bmp1, 0, screen, &dstrect1);
        }
        if(page==1)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1245, 550, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(bmp01, 0, screen, &dstrect01);
            SDL_BlitSurface(bmp1, 0, screen, &dstrect1);
        }
        if(page==2)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1245, 550, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(bmp02, 0, screen, &dstrect02);
            SDL_BlitSurface(bmp1, 0, screen, &dstrect1);
        }
        else if(page==3)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1282, 666, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(bmp8, 0, screen, &dstrect8);
            SDL_BlitSurface(bmp22, 0, screen, &dstrect22);

            SDL_BlitSurface(bmp9, 0, screen, &dstrect9);
            SDL_BlitSurface(bmp19, 0, screen, &dstrect19);
            SDL_BlitSurface(bmp12, 0, screen, &dstrect12);
            if (k==1)
                SDL_BlitSurface(bmp13, 0, screen, &dstrect13);
            else if (k==2)
                SDL_BlitSurface(bmp14, 0, screen, &dstrect14);
            else if (k==3)
                SDL_BlitSurface(bmp15, 0, screen, &dstrect15);
            else if (k==4)
                SDL_BlitSurface(bmp16, 0, screen, &dstrect16);
            else if (k==5)
                SDL_BlitSurface(bmp17, 0, screen, &dstrect17);
            else if (k==6)
                SDL_BlitSurface(bmp18, 0, screen, &dstrect18);
            if (l==(-1))
                SDL_BlitSurface(bmp20, 0, screen, &dstrect20);
            if (l==1)
                SDL_BlitSurface(bmp21, 0, screen, &dstrect21);
        }
        else if(page==4)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1245, 550, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(bmp03, 0, screen, &dstrect03);
        }
        else if(page==5)
        {
            SDL_Surface* screen = SDL_SetVideoMode(1232, 489, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
            SDL_BlitSurface(bmp10, 0, screen, &dstrect10);
        }
        SDL_Flip(screen);
    }
    SDL_FreeSurface(bmp1);

    printf("Exited cleanly\n");
    return 0;
}
